﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessageSystem.Managed.Rpc;


namespace RpcGenerator.AS3
{
    class AS3InterfaceHelperGenerator
    {
        public static void Generate(Type type,IMessageSerializer serializer)
        {
            serializer.NewLine(string.Format("public class {0}Helper", type.Name));
            serializer.BeginBlock();

                serializer.NewLine(string.Format("public function {0}Helper()", type.Name));
                serializer.BeginBlock();
                serializer.EndBlock();

                string proxyname = RpcGeneratorHelper.GetProxyName(type);
                Int16 serviceid = RpcControl.GetServiceId(type);
                string invokerName = RpcGeneratorHelper.GetInvokerName(type);

                serializer.NewLine();
                serializer.NewLine(string.Format("public static function CreateProxy(domain:RpcDomain,sessionid:int,callbackImpl:{0}Callback):{1}",type.Name,proxyname));
                serializer.BeginBlock();
                    serializer.NewLine(string.Format("return domain.CreateProxy({0},{1},sessionid,callbackImpl,{2}.Instance) as {0};",proxyname,serviceid,invokerName));
                serializer.EndBlock();

                serializer.NewLine();
                serializer.NewLine(string.Format("public static function BindService(domain:RpcDomain,sessionid:int,impl:{0}):void",type.Name));
                serializer.BeginBlock();
                    serializer.NewLine(string.Format("domain.BindService({0},sessionid,impl,{1}.Instance);",serviceid,invokerName));
                serializer.EndBlock();

            serializer.EndBlock();
        }
    }
}
